package com.yu.blog.utils.servletUtils;

import com.alibaba.fastjson2.JSONObject;
import com.yu.blog.enums.HttpStatusEnum;
import com.yu.blog.result.Result;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class ResponseUtil {

    public static void setSuccessResponse(HttpServletResponse response, HttpStatusEnum httpStatusEnum) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setStatus(httpStatusEnum.getCode());
        response.getWriter().write(JSONObject.toJSONString(Result.success(httpStatusEnum.getMessage())));
    }

    public static void setFailedResponse(HttpServletResponse response, HttpStatusEnum httpStatusEnum) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setStatus(HttpStatusEnum.FAIL_SUCCESS.getCode());
        response.getWriter().write(JSONObject.toJSONString(Result.failed(httpStatusEnum.getMessage())));
    }

}
